package cn.sym.pet.controller;

import cn.sym.basic.exception.MyException;
import cn.sym.basic.util.AjaxResult;
import cn.sym.basic.util.PageList;
import cn.sym.pet.domain.OrderPetAcquisition;
import cn.sym.pet.dto.OrderConfirmDto;
import cn.sym.pet.query.OrderPetAcquisitionQuery;
import cn.sym.pet.service.IOrderPetAcquisitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {
    @Autowired
    public IOrderPetAcquisitionService orderPetAcquisitionService;


    /**
     * 保存和修改公用的
     * @param orderPetAcquisition  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){
        try {
            if( orderPetAcquisition.getId()!=null)
                orderPetAcquisitionService.update(orderPetAcquisition);
            else
                orderPetAcquisitionService.add(orderPetAcquisition);
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    /*@DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.remove(id);
            return AjaxResult.my();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }*/
	
    //获取用户
    @GetMapping("/{id}")
    public OrderPetAcquisition get(@PathVariable("id")Long id)
    {
        return orderPetAcquisitionService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderPetAcquisition> list(){

        return orderPetAcquisitionService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<OrderPetAcquisition> json(@RequestBody OrderPetAcquisitionQuery query) {
        return orderPetAcquisitionService.queryPage(query);
    }

    /**
     * 确认订单
     * @param dto
     * @return
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderConfirmDto dto){
        try {
            orderPetAcquisitionService.confirm(dto);
            return AjaxResult.my();
        } catch (MyException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.removeOrder(id);
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }


}
